﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：页面框架布局
//
//创建标识：米海波 || 2011-04-01
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hmc.IM.Common.Base;
using Hmc.IM.Common.UserControls;

namespace Hmc.IM.Common.Forms
{
    /// <summary>
    /// 窗体布局
    /// </summary>
    public partial class FrameForm : Form, IBaseForm
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrameForm()
        {
            InitializeComponent();
            //子控件默认不允许透明度颜色，设置透明度颜色前需设置其样式。
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.ImeMode = System.Windows.Forms.ImeMode.KatakanaHalf;
            this.Width = 840;
            this.Height = 434;
        }

        /// <summary>
        /// 权限ID
        /// </summary>
        private int _powerId = -1;

        /// <summary>
        /// 权限ID
        /// </summary>
        public int PowerId
        {
            get
            {
                return _powerId;
            }
            set
            {
                _powerId = value;
            }
        }

        /// <summary>
        /// 是否已经打开一次
        /// </summary>
        private bool isPower = false;
        /// <summary>
        /// 重写OnVisibleChanged
        /// </summary>
        /// <param name="e"></param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (DesignMode) return;
            if (PowerId == -1)
            {
                this.Close();
                //已经打开一次
                if (isPower) return;
                isPower = true;
                Exception ex = new Exception("请对权限ID进行赋值!");
                ex.Source = this.Name;
                throw ex;
            }
            if (!this.IsRight(PowerId) && PowerId != 0)
            {
                //已经打开一次
                if (isPower) return;
                MessageBox.Show("无该模块查看权限,请联系负责人！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                isPower = true;
                this.Close();               
            }
        }

        /// <summary>
        /// 根据权限点判断是否拥有权限
        /// </summary>
        /// <param name="rightID"></param>
        /// <returns></returns>
        public virtual bool IsRight(int rightID)
        {
            var query = from p in LoginUserEntity.PermissionsPoint where p == rightID select p;
            if (query.Count() > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 验证指定集合控件下的数据格式
        /// </summary>
        /// <param name="controls">需验证的数据集合</param>
        /// <returns></returns>
        public bool CheckValidate(Control controls)
        {
            //bool fay = true;
            //foreach (Control ctl in controls.Controls)
            //{
            //    if (ctl is IValidate)
            //    {
            //        IValidate box = ctl as IValidate;
            //        if (!box.Validate())
            //        {
            //            fay = false;
            //        }
            //    }
            //    else if (ctl.Controls.Count > 0)
            //    {
            //        CheckValidate(ctl);
            //    }
            //}
            //return fay;
            return CheckValidate(controls, CheckWay.All);
        }

        /// <summary>
        /// 验证指定集合控件下的数据格式
        /// </summary>
        /// <param name="controls">需验证的数据集合</param>
        /// <param name="way">验证方式</param>
        /// <returns></returns>
        public bool CheckValidate(Control controls, CheckWay way)
        {
            bool fay = true;
            foreach (Control ctl in controls.Controls)
            {
                if (ctl is IValidate)
                {
                    IValidate box = ctl as IValidate;
                    if (!box.Validate())
                    {
                        fay = false;
                    }
                }
                else if (ctl.Controls.Count > 0)
                {
                    CheckValidate(ctl);
                }
                if (fay == false && way == CheckWay.OneByOne)
                {
                    controls.Focus();
                    break;
                }
            }
            return fay;
        }

        /// <summary>
        /// 验证当前窗体集合下的数据格式
        /// </summary>
        /// <returns></returns>
        public bool CheckValidate()
        {
            return CheckValidate(this);
        }
 
    }

    /// <summary>
    /// 验证方式
    /// </summary>
    public enum CheckWay
    {
        /// <summary>
        /// 逐个
        /// </summary>
        OneByOne,
        /// <summary>
        /// 全部
        /// </summary>
        All,
    }
}
